﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GeoTrans;
using Microsoft.WindowsMobile.Samples.Location;
using GpSpotMe.Ign.TileModel;
using WinTest.GpSpotMe.Ign.TileModel;

namespace GpSpotMe
{
    public partial class MainForm : Form
    {
        private Gps gps = new Gps();
        private GpsDeviceState lastGpsState = null;
        private GpsPosition lastGpsPosition = null;
        private EventHandler updateDataDelegate = null;

        public MainForm()
        {
            InitializeComponent();
            gps.DeviceStateChanged += new DeviceStateChangedEventHandler(gps_DeviceStateChanged);
            gps.LocationChanged += new LocationChangedEventHandler(gps_LocationChanged);
            updateDataDelegate = new EventHandler(updateData);
        }

        void gps_LocationChanged(object sender, LocationChangedEventArgs args)
        {
            lastGpsPosition = args.Position;
            Invoke(updateDataDelegate);
        }

        void gps_DeviceStateChanged(object sender, DeviceStateChangedEventArgs args)
        {
            lastGpsState = args.DeviceState;
            Invoke(updateDataDelegate);
        }

        /// <summary>
        /// Actualiza la informacion mostrada
        /// Se ejecuta en el Hilo del UI
        /// </summary>
        private void updateData(object sender, EventArgs e)
        {
            if (!gps.Opened) return;
            gpsDataForm.Datasource = lastGpsPosition;
            gpsDataForm.DataBind();
            gpsSatellitePanel.Datasource = lastGpsPosition;
            gpsSatellitePanel.DataBind();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
            }
            this.Close();
        }

        private void menuItemMap_Click(object sender, EventArgs e)
        {
            mapPanel.Position = lastGpsPosition;
            mapPanel.SpotMe();
            tabControl.SelectedIndex = 1;
        }

        private void menuItemPosition_Click(object sender, EventArgs e)
        {
            tabControl.SelectedIndex = 0 ;
        }
        
        private void MainForm_Closing(object sender, CancelEventArgs e)
        {
            if (gps.Opened)
            {
                gps.Close();
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (gps.Opened)
            {
                menuItemGps.Text = "Stop GPS";
            }
            else
            {
                menuItemGps.Text = "Start GPS";
            }
        }

        private void menuItemGps_Click(object sender, EventArgs e)
        {
            if (!gps.Opened)
            {
                gps.Open();
                menuItemGps.Text = "Stop GPS";
            }
            else
            {
                gps.Close();
                menuItemGps.Text = "Start GPS";
            }
        }

    }
}